<template>
  <div class="institution-container">
    <div class="page-header">
      <h2>机构管理</h2>
      <el-button type="primary" @click="handleAdd">新增机构</el-button>
    </div>
    <div class="page-content">
      <!-- 搜索表单 -->
      <el-form :inline="true" class="search-form">
        <el-form-item label="机构名称">
          <el-input v-model="searchForm.name" placeholder="请输入机构名称" clearable />
        </el-form-item>
        <el-form-item label="机构类型">
          <el-select v-model="searchForm.type" placeholder="请选择机构类型" clearable>
            <el-option label="学校" value="school" />
            <el-option label="培训机构" value="training" />
            <el-option label="艺术团体" value="art_group" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="searchForm.status" placeholder="请选择状态" clearable>
            <el-option label="正常" value="active" />
            <el-option label="禁用" value="disabled" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 表格 -->
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="id" label="机构编号" width="120" />
        <el-table-column prop="name" label="机构名称" min-width="180" />
        <el-table-column prop="type" label="机构类型" width="120">
          <template #default="scope">
            {{ getTypeText(scope.row.type) }}
          </template>
        </el-table-column>
        <el-table-column prop="contact" label="联系人" width="120" />
        <el-table-column prop="phone" label="联系电话" width="120" />
        <el-table-column prop="address" label="地址" min-width="200" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="scope.row.status === 'active' ? 'success' : 'info'">
              {{ scope.row.status === 'active' ? '正常' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="scope">
            <el-button link type="primary" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button link type="primary" @click="handleView(scope.row)">查看</el-button>
            <el-button 
              link 
              :type="scope.row.status === 'active' ? 'danger' : 'success'"
              @click="handleToggleStatus(scope.row)"
            >
              {{ scope.row.status === 'active' ? '禁用' : '启用' }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <!-- 新增/编辑对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogType === 'add' ? '新增机构' : '编辑机构'"
      width="600px"
    >
      <el-form :model="institutionForm" label-width="100px">
        <el-form-item label="机构名称" required>
          <el-input v-model="institutionForm.name" placeholder="请输入机构名称" />
        </el-form-item>
        <el-form-item label="机构类型" required>
          <el-select v-model="institutionForm.type" placeholder="请选择机构类型" style="width: 100%">
            <el-option label="学校" value="school" />
            <el-option label="培训机构" value="training" />
            <el-option label="艺术团体" value="art_group" />
          </el-select>
        </el-form-item>
        <el-form-item label="联系人" required>
          <el-input v-model="institutionForm.contact" placeholder="请输入联系人姓名" />
        </el-form-item>
        <el-form-item label="联系电话" required>
          <el-input v-model="institutionForm.phone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="地址" required>
          <el-input v-model="institutionForm.address" placeholder="请输入机构地址" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input 
            v-model="institutionForm.remark" 
            type="textarea" 
            :rows="3" 
            placeholder="请输入备注信息"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSave">确定</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 查看详情对话框 -->
    <el-dialog
      v-model="detailDialogVisible"
      title="机构详情"
      width="800px"
    >
      <el-descriptions :column="2" border>
        <el-descriptions-item label="机构名称">{{ currentInstitution?.name }}</el-descriptions-item>
        <el-descriptions-item label="机构类型">{{ getTypeText(currentInstitution?.type) }}</el-descriptions-item>
        <el-descriptions-item label="联系人">{{ currentInstitution?.contact }}</el-descriptions-item>
        <el-descriptions-item label="联系电话">{{ currentInstitution?.phone }}</el-descriptions-item>
        <el-descriptions-item label="地址" :span="2">{{ currentInstitution?.address }}</el-descriptions-item>
        <el-descriptions-item label="状态">
          <el-tag :type="currentInstitution?.status === 'active' ? 'success' : 'info'">
            {{ currentInstitution?.status === 'active' ? '正常' : '禁用' }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ currentInstitution?.createTime }}</el-descriptions-item>
        <el-descriptions-item label="备注" :span="2">
          {{ currentInstitution?.remark || '无' }}
        </el-descriptions-item>
      </el-descriptions>

      <!-- 参与展演记录 -->
      <div class="performance-list" v-if="currentInstitution?.performances?.length">
        <h3>参与展演记录</h3>
        <el-table :data="currentInstitution.performances" border style="width: 100%">
          <el-table-column prop="name" label="展演名称" min-width="200" />
          <el-table-column prop="time" label="展演时间" width="180" />
          <el-table-column prop="programCount" label="节目数量" width="100">
            <template #default="scope">
              {{ scope.row.programCount }}个
            </template>
          </el-table-column>
          <el-table-column prop="status" label="状态" width="100">
            <template #default="scope">
              <el-tag :type="getPerformanceStatusType(scope.row.status)">
                {{ getPerformanceStatusText(scope.row.status) }}
              </el-tag>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <template #footer>
        <span class="dialog-footer">
          <el-button @click="detailDialogVisible = false">关闭</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 搜索表单数据
const searchForm = reactive({
  name: '',
  type: '',
  status: ''
})

// 表格数据
const tableData = ref([
  {
    id: 'I001',
    name: '音乐学院',
    type: 'school',
    contact: '张三',
    phone: '13800138000',
    address: '北京市海淀区中关村大街1号',
    status: 'active',
    createTime: '2024-01-01 10:00:00',
    remark: '综合性艺术院校',
    performances: [
      {
        name: '2024年春季音乐会',
        time: '2024-05-01 19:30',
        programCount: 5,
        status: 'registering'
      }
    ]
  },
  {
    id: 'I002',
    name: '艺术培训中心',
    type: 'training',
    contact: '李四',
    phone: '13900139000',
    address: '北京市朝阳区建国路2号',
    status: 'active',
    createTime: '2024-01-02 14:30:00',
    remark: '专业艺术培训机构'
  }
])

// 分页数据
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(100)

// 对话框相关
const dialogVisible = ref(false)
const detailDialogVisible = ref(false)
const dialogType = ref('add')
const institutionForm = reactive({
  name: '',
  type: '',
  contact: '',
  phone: '',
  address: '',
  remark: ''
})
const currentInstitution = ref(null)

// 方法
const handleSearch = () => {
  console.log('搜索条件：', searchForm)
}

const resetForm = () => {
  searchForm.name = ''
  searchForm.type = ''
  searchForm.status = ''
}

const getTypeText = (type) => {
  const map = {
    school: '学校',
    training: '培训机构',
    art_group: '艺术团体'
  }
  return map[type]
}

const getPerformanceStatusType = (status) => {
  const map = {
    pending: 'info',
    registering: 'success',
    finished: 'warning'
  }
  return map[status]
}

const getPerformanceStatusText = (status) => {
  const map = {
    pending: '未开始',
    registering: '报名中',
    finished: '已结束'
  }
  return map[status]
}

const handleAdd = () => {
  dialogType.value = 'add'
  Object.assign(institutionForm, {
    name: '',
    type: '',
    contact: '',
    phone: '',
    address: '',
    remark: ''
  })
  dialogVisible.value = true
}

const handleEdit = (row) => {
  dialogType.value = 'edit'
  Object.assign(institutionForm, row)
  dialogVisible.value = true
}

const handleView = (row) => {
  currentInstitution.value = row
  detailDialogVisible.value = true
}

const handleToggleStatus = (row) => {
  const action = row.status === 'active' ? '禁用' : '启用'
  ElMessageBox.confirm(
    `确定${action}机构"${row.name}"吗？`,
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: row.status === 'active' ? 'warning' : 'success'
    }
  ).then(() => {
    row.status = row.status === 'active' ? 'disabled' : 'active'
    ElMessage.success(`${action}成功`)
  }).catch(() => {})
}

const handleSave = () => {
  // 这里应该添加表单验证
  if (dialogType.value === 'add') {
    tableData.value.push({
      id: 'I' + (tableData.value.length + 1).toString().padStart(3, '0'),
      ...institutionForm,
      status: 'active',
      createTime: new Date().toLocaleString()
    })
    ElMessage.success('添加成功')
  } else {
    // 更新现有数据
    const index = tableData.value.findIndex(item => item.id === institutionForm.id)
    if (index !== -1) {
      Object.assign(tableData.value[index], institutionForm)
      ElMessage.success('更新成功')
    }
  }
  dialogVisible.value = false
}

const handleSizeChange = (val) => {
  pageSize.value = val
  // 重新加载数据
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  // 重新加载数据
}
</script>

<style scoped lang="scss">
.institution-container {
  padding: 20px;

  .page-header {
    margin-bottom: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    h2 {
      margin: 0;
      font-size: 20px;
      font-weight: 500;
    }
  }

  .search-form {
    margin-bottom: 20px;
  }

  .pagination-container {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
  }

  .performance-list {
    margin-top: 20px;
    h3 {
      margin: 10px 0;
      font-size: 16px;
      font-weight: 500;
    }
  }
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style>
